import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../pages/Home.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('../pages/Detail.vue'),
    meta: {
      title: '打卡详情'
    }
  },
  {
    path: '/upload',
    name: 'Upload',
    component: () => import('../pages/Upload.vue'),
    meta: {
      title: '发布打卡'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../pages/Profile.vue'),
    meta: {
      title: '个人中心'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫，设置页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router